package com.book.mapper;

import com.book.entity.Student;
import com.book.entity.Users;
import org.apache.ibatis.annotations.*;

/**
 * @Author zzw2000
 * @Date 2022年08月24日 20:49
 * @Description 描述
 */
@Mapper
public interface UserMapper {
    @Select("select * from users where username=#{username}")
    Users getPasswordByUsername(String username);

    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    @Insert("insert into users(username, password,role) values(#{username},#{password},#{role}) ")
    int addUsers(Users users);

    @Insert("insert into student(uid, name, sex, grade) values(#{uid},#{name},#{sex},#{grade}) ")
    int addStu(@Param("uid") int uid, @Param("name") String name,
               @Param("sex") String sex, @Param("grade") int grade);

    @Select("select sid from student where uid=#{uid}")
    Integer getSidByUid(int uid);

    @Results({
            @Result(column = "sid", property = "sid"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "name", property = "username"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "grade", property = "grade"),
            @Result(column = "password", property = "password"),
            @Result(column = "role", property = "role")
    })
    @Select("select * from student,users where student.uid=users.id and uid=#{uid}")
    Student getStu(int uid);

    @Select("SELECT COUNT(*) FROM student")
    Integer getStudentQuantity();

    @Update("update users set password=#{password}  where id=#{id}")
    void update(@Param("id") Integer id, @Param("password") String password);

    @Update("update users set password = #{pwd} where id=#{uid};")
    void updateUser(@Param("uid") Integer uid, @Param("pwd") String pwd);

    @Update("update student set sex = #{sex}, grade=#{grade} where sid=#{sid}")
    void updateStu(@Param("sid") Integer sid, @Param("sex") String sex, @Param("grade") Integer grade);
}
